import tensorflow as tf

mnist = tf.keras.datasets.mnist

# 载入并准备好 MNIST 数据集。将样本从整数转换为浮点数
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train, x_test = x_train / 255.0, x_test / 255.0

# 将模型的各层堆叠起来，以搭建 tf.keras.Sequential 模型。为训练选择优化器和损失函数
model = tf.keras.models.Sequential([
  #   input_shape
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  #   activation
  tf.keras.layers.Dense(128, 'relu'),
  tf.keras.layers.Dropout(0.2),
  #   activation
  tf.keras.layers.Dense(10, 'softmax')
])
# optimizer loss  metrics
model.compile('adam',
              'sparse_categorical_crossentropy',
              ['accuracy'])

# 训练并验证模型
# epochs
model.fit(x_train, y_train, 5)
# verbose
model.evaluate(x_test,  y_test, 2)